import { searchAll } from '../../../api/navigation'
import { getSearchCondition } from '../../../api/base'
Page({
  data: {
    listHeight: 0,

    activeName: '',
    keyword: '',
    navTagsId: '',
    condition: {},

    isNoData: false,
    typeList: [],
    list: [],

    designFilterMap: '',
    activityFilterMap: '',
    liveFilterMap: '',
    materialFilterMap: '',

    designCondition: {},
    activityCondition: {},
    liveCondition: {},
    materialCondition: {},
  },

  options: {
    styleIsolation: 'shared'
  },

  onLoad: async function (options) {
    const listHeight = await this.setListHeight()
    this.setData({
      listHeight: listHeight,
      keyword: options.keyword,
      navTagsId: options.navTagsId
    });
    let condition = {
      filterMap: {},
      searchBizTypeEnum: this.data.activeName,
      navTagsId: '',
      keyWord: this.data.keyword
    }
    if (!this.data.navTagsId) {
      condition.navTagsId = '';
      condition.keyWord = this.data.keyword;
    } else {
      condition.navTagsId = this.data.navTagsId;
      condition.keyWord = '';
    }
    this.setData({
      condition: condition
    })
    this.init({
      condition: condition,
      pager: {
        page: 1,
        pageSize: 1
      }
    });
  },
  setListHeight() {
    const { windowHeight } = wx.getSystemInfoSync();
    return new Promise(resolve => {
      const _this = this
      const query = wx.createSelectorQuery().in(this)
      query.select('#navBar').boundingClientRect();
      query.exec((res) => {
        resolve(windowHeight - res[0].height - 88)
      })
    })
  },
  init({condition, pager}) {
    searchAll({
      condition: condition,
      pager: pager
    }).then(res => {
      if (!res.page) {
        this.setData({
          isNoData: true
        })
      } else {
        this.setData({
          typeList: res.typeList,
          list: res.page.records,
          activeName: res.typeList[0].typeEnum
        })
        res.typeList.forEach(type => {
          getSearchCondition(type.typeEnum).then(con => {
            switch(type.typeEnum) {
              case 'PREPARE':
                this.setData({ designCondition: { selectCondition: con, keyword: this.data.keyword, navTagsId: this.data.navTagsId} });
                break;
              case 'TEACHINGRESEARCH':
                this.setData({ activityCondition: { selectCondition: con, keyword: this.data.keyword, navTagsId: this.data.navTagsId}});
                break;
              case 'WEB_CAST':
                this.setData({ liveCondition: { selectCondition: con, keyword: this.data.keyword, navTagsId: this.data.navTagsId} });
                break;
              case 'MATERIAL':
                this.setData({ materialCondition: { selectCondition: con, keyword: this.data.keyword, navTagsId: this.data.navTagsId} });
                break;
            }
          })
        })
      }
    })
  },

  changeCondition(e) {

  },

  tapTabs(e) {
    this.setData({
      activeName: e.detail.name
    })
    const designComponent = this.selectComponent('#designComponent');
    const activityComponent = this.selectComponent('#activityComponent');
    const liveComponent = this.selectComponent('#liveComponent');
    const materialComponent = this.selectComponent('#materialComponent');
    if (designComponent) {
      designComponent.cancelSelectCondition()
    }
    if (activityComponent) {
      activityComponent.cancelSelectCondition()
    }
    if (liveComponent) {
      liveComponent.cancelSelectCondition()
    }
    if (materialComponent) {
      materialComponent.cancelSelectCondition()
    }
  },

  keywordSearch(e) {
    this.setData({
      keyword: e.detail,
      'condition.keyWord': e.detail,
      'designCondition.keyword': e.detail,
      'activityCondition.keyword': e.detail,
      'liveCondition.keyword': e.detail,
      'materialCondition.keyword': e.detail
    })
  },

  back() {
    wx.navigateBack({
      delta: 1
    })
  },

  toPageMessage() {
    wx.navigateTo({
      url: '/packageA/pages/message/message'
    })
  }
})
